﻿using SCG.General;
using System;
using System.Net;

namespace SCG.SolarVengeanceEngine.StarShips
{
    //MadBombs
    public class MadBomb : StarShip
    {
        //constructor
        public MadBomb(SVGame game, Player owner, int engines, int bombRange, StarSystem origin)
            : base(game, owner, engines, bombRange, origin)
        {
        }

        //ship name
        public override string Name
        {
            get
            {
                return "MadBomb";
            }
        }

        //description
        public override string Description
        {
            get
            {
                return "When MadBombs detonate they damage all StarShips in their Blast Radius, friendly or enemy.  The maximum amount of damage is also determined by the Blast Radius.  Any player who identifies a MadBomb on the map can detonate it.";
            }
        }

        //ship type
        public override StarShipType ShipType
        {
            get
            {
                return StarShipType.MadBomb;
            }
        }

        //They can detonate
        public override bool CanExecuteOrder(OrderType ot)
        {
            switch (ot)
            {
                case OrderType.Detonate:
                    return true;
                default:
                    return base.CanExecuteOrder(ot);
            }
        }

        //Detonate the MadBomb
        internal void Detonate()
        {
            //prevent multiple detonations
            if (Destroyed)
                return;

            //damage all StarShips in range
            foreach (StarShip ship in Game.StarShips)
                if (Distance(ship) <= Value)
                    if (ship != this && !ship.Destroyed)
                    {
                        int damage = SVGame.Random(1, Value);
                        ship.Damage(damage, true);                       
                    }

            //and destroy the MadBomb
            Game.DestroyStarShip(this);
        }

        //chance of detonation upon damage
        protected internal override void ProcessDamage(int damage, bool canDamageEngines)
        {
            base.ProcessDamage(damage, canDamageEngines);
            if (!Destroyed)
            {
                if (SVGame.Random(1, 100) < 25)
                    Detonate();
            }
        }

        //draw bomb radius
        public override SCGColor RadiusColor
        {
            get
            {
                return BombRangeColor;
            }
        }

        //private members
        private static SCGColor BombRangeColor = new SCGColor(96, 255, 128, 0);
    }
}
